package com.shaw.workrecord.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shaw.workrecord.dto.SystemUser;
import com.shaw.workrecord.exception.CustomerException;
import com.shaw.workrecord.mapper.SystemUserMapper;
import com.shaw.workrecord.pojo.request.RegisterRequest;
import com.shaw.workrecord.service.IRegisterService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.UUID;

/**
 * @author jh.jiang
 * date 2022-08-28
 * description
 */
@Service
public class RegisterServiceImpl extends ServiceImpl<SystemUserMapper, SystemUser> implements IRegisterService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean register(RegisterRequest request) {
        LambdaQueryWrapper<SystemUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.select().eq(SystemUser::getAccount, request.getAccount());
        SystemUser systemUser = baseMapper.selectOne(wrapper);
        if (systemUser == null) {
            SystemUser user = new SystemUser();
            user.setAccount(request.getAccount());
            if (ObjectUtil.isEmpty(request.getNickName())) {
                user.setNickName(UUID.randomUUID().toString());
            } else {
                user.setNickName(request.getNickName());
            }
            user.setPhone(request.getAccount());
            user.setPassword(request.getPassword());
            int result = baseMapper.insert(user);
            return result > 0;
        }else {
            throw new CustomerException("用户已存在");
        }
    }
}
